// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../component/ComponentBase";
import DataMgr from "../mgr/dataManager";
import GameMgr from "../mgr/gameManager";
import NativeMgr from "../mgr/nativeManager";
import NetMgr from "../mgr/netManager";



const { ccclass, property } = cc._decorator;

@ccclass
export default class MsgLayer extends ComponentBase {

    coin: cc.Node = null;
    closeAll: cc.Node = null;
    redPack1: cc.Node = null;
    redPack2: cc.Node = null;
    getCoin: cc.Node = null;
    updateAd: boolean = false;

    onLoad(): void {
        super.onLoad();

        this.scheduleOnce(() => {
            this.closeAll.active = true;
        }, 5);


        this.initEvent()
        NativeMgr.Ins().showFeedAds();
        console.log('coin1 = ', DataMgr.Ins().userInfo.redPack);
        const coin = Math.floor(DataMgr.Ins().userInfo.redPack / 10);
        console.log('coin = ', coin)
        this.coin.getComponent(cc.Label).string = Math.min(coin, 5000);

        if (coin < 3500) {
            this.initLittle()
        } else {
            this.initNormal()
        }



    }

    initEvent() {
        cc.systemEvent.on("getEcpm", this.onAdClose.bind(this), this);
    }

    onAdClose() {
        if (this.updateAd) {
            const coin = Math.floor(DataMgr.Ins().userInfo.redPack / 10);
            this.coin.getComponent(cc.Label).string = Math.min(coin, 5000);
            this.updateAd = false;
        }
    }

    closeAllCallBack() {
        NativeMgr.Ins().delFeedAds();
        this.node.removeFromParent(true);
        this.node.destroy();
        NativeMgr.Ins().showExpressAds(0);
    }

    initLittle() {
        this.redPack1.active = false;
        this.redPack2.active = true;
        this.getCoin.active = false;
    }

    initNormal() {
        this.redPack1.active = true;
        this.redPack2.active = false;
        this.getCoin.active = false;
    }


    initGet() {
        this.redPack1.active = false;
        this.redPack2.active = false;
        this.getCoin.active = true;
    }


    normalGetCallBack() {
        NativeMgr.Ins().showAds(1);
        this.initGet();
    }

    littleGetCallBack() {
        NativeMgr.Ins().showAds(1);
        this.initGet();
    }

    addCoinCallBack() {
        this.updateAd = true;
        NativeMgr.Ins().showAds(1);

    }


    async getBtnCallBack() {




        const coin = this.coin.getComponent(cc.Label).string;

        const changeEcpm = Math.min(DataMgr.Ins().userInfo.redPack, 50000);

        const infoTotal = await NetMgr.Ins().servo.get(`/profile/exchange/${DataMgr.Ins().token}?ecpm=${changeEcpm}`);

        if (infoTotal.status == 200) {
            const data = infoTotal.body.user;

            console.log('initUserData data = ', data);
            DataMgr.Ins().userInfo.coin = data.coin;
            DataMgr.Ins().userInfo.redPack = Number(data.ecpm);
        } else {
            GameMgr.Ins().showMsg('profile/exchange' + infoTotal.status);
        }

        // DataMgr.Ins().userInfo.coin += Number(coin);
        // DataMgr.Ins().userInfo.redPack -= Number(coin);
        NativeMgr.Ins().delFeedAds();
        this.node.removeFromParent(true);
        this.node.destroy();
        NativeMgr.Ins().showExpressAds(0);
    }

    onDestroy(): void {
        super.onDestroy();
        cc.systemEvent.targetOff(this);

    }




    // update (dt) {}
}
